﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.IO;
using WebDS;
using WebUS;
using WebDS.CDBNames;

using IP.Core.IPCommon;
using IP.Core.IPUserService;
using IP.Core.IPData;
using System.Data;
using System.Text;
using System.Diagnostics;
using System.Configuration;

public partial class ChucNang_F303_HoSoDetail : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        m_lbl_thong_bao.Text = "";
        if (!IsPostBack)
        {
            m_cmd_cap_nhat_pl.Enabled = true;
            // show on grid
            if (Request.QueryString["id_gv"] != null)
            {
                hdf_id_giang_vien.Value = Request.QueryString["id_gv"];
                load_data_2_basic_control();
                load_all_hs_dinh_kem();
                load_trang_thai_ho_so_giang_vien_grid();
            }
        }
    }

    #region Public Interface
    public string get_mapping_ten_giang_vien(object i_dc_id_giang_vien)
    {
        string v_str_ten_giang_vien = "";
        try
        {
            US_V_DM_GIANG_VIEN v_us_dm_giang_vien = new US_V_DM_GIANG_VIEN(CIPConvert.ToDecimal(i_dc_id_giang_vien));
            v_str_ten_giang_vien = v_us_dm_giang_vien.strHO_VA_TEN_DEM.TrimEnd() + " " + v_us_dm_giang_vien.strTEN_GIANG_VIEN;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
        return v_str_ten_giang_vien;
    }
    public string get_mapping_ten_loai_ho_so(object i_dc_id_loai_ho_so)
    {
        string v_str_ten_loai_ho_so = "";
        try
        {
            US_CM_DM_TU_DIEN v_us_dm_tu_dien = new US_CM_DM_TU_DIEN(CIPConvert.ToDecimal(i_dc_id_loai_ho_so));
            v_str_ten_loai_ho_so = v_us_dm_tu_dien.strTEN;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
        return v_str_ten_loai_ho_so;
    }
    public string get_mapping_loai_giang_vien(object i_dc_id_giang_vien)
    {
        string v_str_loai_giang_vien = "";
        try
        {
            US_V_DM_GIANG_VIEN v_us_dm_giang_vien = new US_V_DM_GIANG_VIEN(CIPConvert.ToDecimal(i_dc_id_giang_vien));
            v_str_loai_giang_vien = v_us_dm_giang_vien.strGVHD_YN.Trim() + v_us_dm_giang_vien.strGVCM_YN.Trim();
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
        return v_str_loai_giang_vien;
    }

    public string get_mapping_ten_don_vi_thanh_toan(object i_dc_id_don_vi_thanh_toan)
    {
        string v_str_ten_don_vi_thanh_toan = "";
        try
        {
            US_DM_DON_VI_THANH_TOAN v_us_dm_don_vi_thanh_toan = new US_DM_DON_VI_THANH_TOAN(CIPConvert.ToDecimal(i_dc_id_don_vi_thanh_toan));
            v_str_ten_don_vi_thanh_toan = v_us_dm_don_vi_thanh_toan.strTEN_DON_VI;
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
        return v_str_ten_don_vi_thanh_toan;
    }
    public string get_mapping_ma_to_ten_trang_thai(object ip_ma_tu_dien)
    {
        US_CM_DM_TU_DIEN v_us_cm_dm = new US_CM_DM_TU_DIEN(CIPConvert.ToDecimal(ip_ma_tu_dien));
        return v_us_cm_dm.strTEN;
    }
    private string get_ho_ten_gv_ko_dau()
    {
        string v_str_ten_gv = "";

        if (hdf_id_giang_vien.Value != "")
        {
            US_V_DM_GIANG_VIEN v_us_dm_gv = new US_V_DM_GIANG_VIEN(CIPConvert.ToDecimal(hdf_id_giang_vien.Value));
            v_str_ten_gv = v_us_dm_gv.strMA_GIANG_VIEN + "_" + v_us_dm_gv.strHO_VA_TEN_DEM + " " + v_us_dm_gv.strTEN_GIANG_VIEN;
            v_str_ten_gv = v_str_ten_gv.Replace(" ", "");
            v_str_ten_gv = Change_AV(v_str_ten_gv);
            hdf_ten_gv_giang_vien.Value = Change_AV((v_us_dm_gv.strHO_VA_TEN_DEM + v_us_dm_gv.strTEN_GIANG_VIEN).Replace(" ", ""));
        }
        hdf_ten_folder_gv.Value = v_str_ten_gv;
        return v_str_ten_gv;
    }
    #endregion

    #region Members
    DataEntryFormMode m_init_mode = DataEntryFormMode.ViewDataState;
    DS_GD_HO_SO_GV_DETAIL m_ds_gd_ho_so_gv_detail = new DS_GD_HO_SO_GV_DETAIL();
    US_GD_HO_SO_GV_DETAIL m_us_gd_ho_so_gv_detail = new US_GD_HO_SO_GV_DETAIL();
    public string m_str_table_trang_thai_hs_gv_dv_quan_ly = "";
    US_GD_TRANG_THAI_HO_SO_GV m_us_gd_trang_thai_hs_giang_vien = new US_GD_TRANG_THAI_HO_SO_GV();
    DS_GD_TRANG_THAI_HO_SO_GV m_ds_gd_trang_thai_hs_giang_vien = new DS_GD_TRANG_THAI_HO_SO_GV();
    #endregion

    #region Private Method
    private void reset_control()
    {
        lblHoSoDinhKem13.Visible = false;
        lblHoSoDinhKem12.Visible = false;
    }
    // Thực hiện toàn bộ việc upload hồ sơ lên Folder riêng của từng Giảng viên
    private void Upload_dinh_kem_ho_so()
    {
        string v_str_folder_gv = get_ho_ten_gv_ko_dau();
        string v_str_uploadFolder = Request.PhysicalApplicationPath + "HoSoDinhKem\\" + v_str_folder_gv + "\\";
        string v_str_extension = "";
        string v_str_ten_file = "";
        // Nếu Folder Giảng viên chưa tồn tại
        if (!Directory.Exists(v_str_uploadFolder))
        {
            Directory.CreateDirectory(v_str_uploadFolder);
        }
        // Hồ sơ loại 1.1
        if (m_up_ho_so_loai_1_dk1.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_1_dk1.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_1_dk1.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_1_dk1.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_11.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }

        // Hồ sơ loại 1.2
        if (m_up_ho_so_loai_1_dk2.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_1_dk2.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_1_dk2.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_1_dk2.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_12.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }

        // Hồ sơ loại 1.3
        if (m_up_ho_so_loai_1_dk3.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_1_dk3.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_1_dk3.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_1_dk3.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_13.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }

        // Hồ sơ loại 2.1
        if (m_up_ho_so_loai_2_dk1.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_2_dk1.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_2_dk1.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_2_dk1.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_21.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }
        // Hồ sơ loại 2.2
        if (m_up_ho_so_loai_2_dk2.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_2_dk2.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_2_dk2.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_2_dk2.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_22.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }
        // Hồ sơ loại 2.3
        if (m_up_ho_so_loai_2_dk3.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_2_dk3.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_2_dk3.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_2_dk3.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_23.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }

        // Hồ sơ loại 3.1
        if (m_up_ho_so_loai_3_dk1.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_3_dk1.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_3_dk1.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_3_dk1.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_31.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }
        // Hồ sơ loại 3.2
        if (m_up_ho_so_loai_3_dk2.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_3_dk2.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_3_dk2.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_3_dk2.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_32.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }
        // Hồ sơ loại 3.3
        if (m_up_ho_so_loai_3_dk3.HasFile)
        {
            v_str_extension = Path.GetExtension(m_up_ho_so_loai_3_dk3.PostedFile.FileName);
            v_str_ten_file = get_file_name(Path.GetFileNameWithoutExtension(m_up_ho_so_loai_3_dk3.PostedFile.FileName));
            if (v_str_ten_file.Trim() != "")
            {
                m_up_ho_so_loai_3_dk3.SaveAs(v_str_uploadFolder + v_str_ten_file + v_str_extension);
                hdf_upload_33.Value = v_str_folder_gv + "/" + v_str_ten_file + v_str_extension;
            }
        }
    }
    private string Change_AV(string ip_str_change)
    {
        Regex v_reg_regex = new Regex("\\p{IsCombiningDiacriticalMarks}+");
        string v_str_FormD = ip_str_change.Normalize(NormalizationForm.FormD);
        return v_reg_regex.Replace(v_str_FormD, String.Empty).Replace('\u0111', 'd').Replace('\u0110', 'D');
    }
    private string get_file_name(string ip_str_ten_file)
    {
        string v_str_file_name;
        v_str_file_name = hdf_ten_gv_giang_vien.Value + "_" + Change_AV(ip_str_ten_file) + "_ver" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + "_" + DateTime.Now.Hour + DateTime.Now.Minute;
        v_str_file_name = v_str_file_name.Replace(" ", "_");
        v_str_file_name = v_str_file_name.Replace("-", "_");
        return v_str_file_name;
    }
    private bool check_exist_giang_vien_dv_tt(decimal ip_id_ho_so, string ip_str_loai_hs, out string op_str_du_an_trung)
    {
        m_us_gd_ho_so_gv_detail.check_trung_giang_vien_dvtt(m_ds_gd_ho_so_gv_detail, ip_id_ho_so, ip_str_loai_hs);
        op_str_du_an_trung = "";
        if (m_ds_gd_ho_so_gv_detail.GD_HO_SO_GV_DETAIL.Rows.Count > 0)
        {
            op_str_du_an_trung = "Đã tồn tại bản ghi này.";
            return false;
        }
        return true;
    }
    private void us_object_2_form(US_GD_HO_SO_GV_DETAIL ip_us_gd_ho_so_gv_detail)
    {
        try
        {
            lblHoSoDinhKem13.Visible = true;
            lblHoSoDinhKem12.Visible = true;
            lblHoSoDinhKem12.Text = ip_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM;
        }
        catch (Exception v_e)
        {

            throw v_e;
        }

    }
    // Load data và hiển thị lên form
    private void load_data_2_us_by_id_and_show_on_form(int ip_i_ho_so_selected)
    {
        try
        {
            //decimal v_dc_id_loai_ho_so = CIPConvert.ToDecimal(m_grv_gd_ho_so_detail.DataKeys[ip_i_ho_so_selected].Value);
            //US_GD_HO_SO_GV_DETAIL v_us_gd_ho_so_gv_detail = new US_GD_HO_SO_GV_DETAIL(v_dc_id_loai_ho_so);

            //// Load data to form 
            //us_object_2_form(v_us_gd_ho_so_gv_detail);
        }
        catch (Exception v_e)
        {
            throw v_e;
        }

    }
    private void delete_row_ho_so_gv_detail(int ip_i_id_ho_so)
    {
        //decimal v_dc_id_ho_so_gv_detail = CIPConvert.ToDecimal(m_grv_gd_ho_so_detail.DataKeys[ip_i_id_ho_so].Value);
        //m_us_gd_ho_so_gv_detail.dcID = v_dc_id_ho_so_gv_detail;
        //m_us_gd_ho_so_gv_detail.DeleteByID(v_dc_id_ho_so_gv_detail);
        //m_lbl_thong_bao.Text = "Xóa bản ghi thành công";
        //load_data_2_grid(CIPConvert.ToDecimal(Request.QueryString["id_hs"]));
    }
    // Load data to so hợp đồng và tên giảng viên
    private void load_data_2_basic_control()
    {
        try
        {
            // Load thông tin giảng viên
            decimal v_dc_id_gv = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            US_V_DM_GIANG_VIEN v_us_v_dm_giang_vien = new US_V_DM_GIANG_VIEN(v_dc_id_gv);
            m_lbl_giang_vien.Text = v_us_v_dm_giang_vien.strHO_VA_TEN_DEM.Trim() + " " + v_us_v_dm_giang_vien.strTEN_GIANG_VIEN.Trim();
            m_lbl_ma_giang_vien.Text = v_us_v_dm_giang_vien.strMA_GIANG_VIEN;

            // Load thông tin hồ sơ
            US_CM_DM_TU_DIEN v_us_cmd_dm_tu_dien = new US_CM_DM_TU_DIEN();
            DS_CM_DM_TU_DIEN v_ds_cmd_dm_tu_dien = new DS_CM_DM_TU_DIEN();
            v_us_cmd_dm_tu_dien.fill_tu_dien_cung_loai_ds(LOAI_TU_DIEN.LOAI_HO_SO, v_ds_cmd_dm_tu_dien);

            // load vào các loại hồ sơ
            m_lbl_loai_hs_1.Text = CIPConvert.ToStr(v_ds_cmd_dm_tu_dien.CM_DM_TU_DIEN.Rows[0][CM_DM_TU_DIEN.TEN]);
            m_lbl_loai_hs_1.ToolTip = CIPConvert.ToStr(v_ds_cmd_dm_tu_dien.CM_DM_TU_DIEN.Rows[0][CM_DM_TU_DIEN.ID]);

            m_lbl_loai_hs_2.Text = CIPConvert.ToStr(v_ds_cmd_dm_tu_dien.CM_DM_TU_DIEN.Rows[1][CM_DM_TU_DIEN.TEN]);
            m_lbl_loai_hs_2.ToolTip = CIPConvert.ToStr(v_ds_cmd_dm_tu_dien.CM_DM_TU_DIEN.Rows[1][CM_DM_TU_DIEN.ID]);

            m_lbl_loai_hs_3.Text = CIPConvert.ToStr(v_ds_cmd_dm_tu_dien.CM_DM_TU_DIEN.Rows[2][CM_DM_TU_DIEN.TEN]);
            m_lbl_loai_hs_3.ToolTip = CIPConvert.ToStr(v_ds_cmd_dm_tu_dien.CM_DM_TU_DIEN.Rows[2][CM_DM_TU_DIEN.ID]);

        }
        catch (Exception v_e)
        {

            //throw v_e;
        }
    }
    // Lấy được i nội dung thanh toán từ id phụ lục đã biết
    private string get_id_ho_so_gv_by_id_detail(decimal ip_dc_id_detail)
    {
        US_GD_HO_SO_GV_DETAIL v_us_gd_ho_so_gv_detail = new US_GD_HO_SO_GV_DETAIL(ip_dc_id_detail);
        if (v_us_gd_ho_so_gv_detail.IsIDNull()) return "";
        return v_us_gd_ho_so_gv_detail.strTEN_LOAI_HO_SO;
    }
    private void load_trang_thai_ho_so_giang_vien_grid()
    {
        m_us_gd_trang_thai_hs_giang_vien.dcID_GIANG_VIEN = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
        m_us_gd_trang_thai_hs_giang_vien.load_trang_thai_hs_giang_vien(m_ds_gd_trang_thai_hs_giang_vien);
        m_grv_dm_ho_so.DataSource = m_ds_gd_trang_thai_hs_giang_vien.GD_TRANG_THAI_HO_SO_GV;
        m_grv_dm_ho_so.DataBind();
    }
    // load các đính kèm nếu đã upload
    private void load_all_hs_dinh_kem()
    {
        DS_GD_HO_SO_GV_DETAIL v_ds_ho_so_gv_detail = new DS_GD_HO_SO_GV_DETAIL();
        US_GD_HO_SO_GV_DETAIL v_us_ho_so_gv_detail = new US_GD_HO_SO_GV_DETAIL();
        v_us_ho_so_gv_detail.load_ho_so_detail_giang_vien(CIPConvert.ToDecimal(hdf_id_giang_vien.Value), v_ds_ho_so_gv_detail);

        DataRow[] v_dr_loai_1 = v_ds_ho_so_gv_detail.GD_HO_SO_GV_DETAIL.Select(GD_HO_SO_GV_DETAIL.ID_LOAI_HS + " = " + m_lbl_loai_hs_1.ToolTip);
        DataRow[] v_dr_loai_2 = v_ds_ho_so_gv_detail.GD_HO_SO_GV_DETAIL.Select(GD_HO_SO_GV_DETAIL.ID_LOAI_HS + " = " + m_lbl_loai_hs_2.ToolTip);
        DataRow[] v_dr_loai_3 = v_ds_ho_so_gv_detail.GD_HO_SO_GV_DETAIL.Select(GD_HO_SO_GV_DETAIL.ID_LOAI_HS + " = " + m_lbl_loai_hs_3.ToolTip);

        if (v_dr_loai_1.Length > 0)
        {
            m_link_dinh_kem11.Visible = true;
            m_link_dinh_kem11.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_1[0][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
            m_link_dinh_kem11.ToolTip = v_dr_loai_1[0][GD_HO_SO_GV_DETAIL.ID].ToString();
            
            if (v_dr_loai_1.Length > 1)
            {
                m_link_dinh_kem12.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_1[1][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
                m_link_dinh_kem12.ToolTip = v_dr_loai_1[1][GD_HO_SO_GV_DETAIL.ID].ToString();
                m_link_dinh_kem12.Visible = true;
            }
            if (v_dr_loai_1.Length > 2)
            {
                m_link_dinh_kem13.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_1[2][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
                m_link_dinh_kem13.ToolTip = v_dr_loai_1[2][GD_HO_SO_GV_DETAIL.ID].ToString();
                m_link_dinh_kem13.Visible = true;
            }
        }
        if (v_dr_loai_2.Length > 0)
        {
            m_link_dinh_kem21.Visible = true;
            m_link_dinh_kem21.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_2[0][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
            m_link_dinh_kem21.ToolTip = v_dr_loai_2[0][GD_HO_SO_GV_DETAIL.ID].ToString();
            if (v_dr_loai_2.Length > 1)
            {
                m_link_dinh_kem22.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_2[1][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
                m_link_dinh_kem22.ToolTip = v_dr_loai_2[1][GD_HO_SO_GV_DETAIL.ID].ToString();
                m_link_dinh_kem22.Visible = true;
            }
            if (v_dr_loai_2.Length > 2)
            {
                m_link_dinh_kem23.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_2[2][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
                m_link_dinh_kem23.ToolTip = v_dr_loai_2[2][GD_HO_SO_GV_DETAIL.ID].ToString();
                m_link_dinh_kem23.Visible = true;
            }
        }
        if (v_dr_loai_3.Length > 0)
        {
            m_link_dinh_kem31.Visible = true;
            m_link_dinh_kem31.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_3[0][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
            m_link_dinh_kem31.ToolTip = v_dr_loai_3[0][GD_HO_SO_GV_DETAIL.ID].ToString();
            
            if (v_dr_loai_3.Length > 1)
            {
                m_link_dinh_kem32.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_3[1][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
                m_link_dinh_kem32.ToolTip = v_dr_loai_3[1][GD_HO_SO_GV_DETAIL.ID].ToString();
                m_link_dinh_kem32.Visible = true;
            }
            if (v_dr_loai_3.Length > 2)
            {
                m_link_dinh_kem33.NavigateUrl = "\\TRMProject\\HoSoDinhKem\\" + v_dr_loai_3[2][GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM].ToString();
                m_link_dinh_kem33.ToolTip = v_dr_loai_3[2][GD_HO_SO_GV_DETAIL.ID].ToString();
                m_link_dinh_kem33.Visible = true;
            }
        }
    }
    private void cap_nhat_trang_thai_gv_theo_don_vi_qly()
    {
        foreach (GridViewRow row in m_grv_dm_ho_so.Rows)
        {
            bool isVanBangCCChecked = ((CheckBox)row.FindControl("chk_van_bang_cong_chung")).Checked;
            bool isCCSPhecked = ((CheckBox)row.FindControl("chk_ccsp")).Checked;
            bool isLyLichKHChecked = ((CheckBox)row.FindControl("chk_ly_lich_khoa_hoc")).Checked;

            Label lblDonViQuanLy = ((Label)row.FindControl("DonViQuanLy"));
            
            m_us_gd_trang_thai_hs_giang_vien.dcID_GIANG_VIEN = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            m_us_gd_trang_thai_hs_giang_vien.dcID_DV_QUAN_LY = CIPConvert.ToDecimal(lblDonViQuanLy.Text.Trim());
            m_us_gd_trang_thai_hs_giang_vien.strVAN_BANG_CC_YN = isVanBangCCChecked ? "Y" : "N";
            m_us_gd_trang_thai_hs_giang_vien.strCCSP_YN = isCCSPhecked ? "Y" : "N";
            m_us_gd_trang_thai_hs_giang_vien.strLY_LICH_KH_YN = isLyLichKHChecked ? "Y" : "N";
            m_us_gd_trang_thai_hs_giang_vien.Update();
        }
        load_trang_thai_ho_so_giang_vien_grid();
        m_lbl_thong_bao.Text = "Cập nhật trạng thái hồ sơ cho giảng viên thành công!";
    }
    private void update_file_ho_so_dinh_kem()
    {
        // Insert các file mới
        DataTable v_data_ho_so_detail = new DataTable(US_GD_HO_SO_GV_DETAIL.c_TableName);
        v_data_ho_so_detail.Columns.Add(GD_HO_SO_GV_DETAIL.ID);
        v_data_ho_so_detail.Columns.Add(GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN);
        v_data_ho_so_detail.Columns.Add(GD_HO_SO_GV_DETAIL.ID_LOAI_HS);
        //v_data_ho_so_detail.Columns.Add(GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT);
        v_data_ho_so_detail.Columns.Add(GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO);
        v_data_ho_so_detail.Columns.Add(GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM);

        decimal v_dc_hs_id = m_us_gd_ho_so_gv_detail.get_last_index_row_ho_so_detail();
        string v_str_conn_string = "data source = " + ConfigurationManager.AppSettings["SERVER"] + ";"
                                    + "initial catalog= " + ConfigurationManager.AppSettings["INITIAL_DATABASE"] + ";"
                                    + "user id= " + ConfigurationManager.AppSettings["INITIAL_USER"] + ";"
                                    + "password=" + ConfigurationManager.AppSettings["PASS_WORD"];
        if (m_up_ho_so_loai_1_dk1.HasFile 
            && m_link_dinh_kem11.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_1.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_1.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_11.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }
        if (m_up_ho_so_loai_1_dk2.HasFile
            && m_link_dinh_kem12.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_1.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_1.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_12.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }
        if ((m_up_ho_so_loai_1_dk3.HasFile)
            && m_link_dinh_kem13.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_1.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_1.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_13.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }

        if ((m_up_ho_so_loai_2_dk1.HasFile)
            && m_link_dinh_kem21.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_2.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_2.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_21.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }
        if ((m_up_ho_so_loai_2_dk2.HasFile)
            && m_link_dinh_kem22.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_2.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_2.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_22.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }
        if ((m_up_ho_so_loai_2_dk3.HasFile)
            && m_link_dinh_kem23.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_2.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_2.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_23.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }

        if ((m_up_ho_so_loai_3_dk1.HasFile)
            && m_link_dinh_kem31.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_3.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_3.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_31.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }
        if ((m_up_ho_so_loai_3_dk2.HasFile)
            && m_link_dinh_kem32.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_3.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_3.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_32.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }
        if ((m_up_ho_so_loai_3_dk3.HasFile)
            && m_link_dinh_kem33.Visible == false)
        {
            DataRow v_hs_row = v_data_ho_so_detail.NewRow();
            v_dc_hs_id = v_dc_hs_id + 1;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID] = v_dc_hs_id;
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_GIANG_VIEN] = CIPConvert.ToDecimal(hdf_id_giang_vien.Value);
            v_hs_row[GD_HO_SO_GV_DETAIL.ID_LOAI_HS] = CIPConvert.ToDecimal(m_lbl_loai_hs_3.ToolTip);
            //v_hs_row[GD_HO_SO_GV_DETAIL.NGAY_CAP_NHAT] = Convert.ToDateTime("01/01/1900");
            v_hs_row[GD_HO_SO_GV_DETAIL.TEN_LOAI_HO_SO] = m_lbl_loai_hs_3.Text;
            v_hs_row[GD_HO_SO_GV_DETAIL.HO_SO_DINH_KEM] = hdf_upload_33.Value;

            v_data_ho_so_detail.Rows.Add(v_hs_row);
        }

        m_us_gd_ho_so_gv_detail.insert_ho_so_detail(v_str_conn_string, v_data_ho_so_detail, US_GD_HO_SO_GV_DETAIL.c_TableName);
        
        // Update các file mới được upload lại
        m_us_gd_ho_so_gv_detail.BeginTransaction();
        
        if (m_up_ho_so_loai_1_dk1.HasFile
            && m_link_dinh_kem11.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem11.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_11.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }

        if (m_up_ho_so_loai_1_dk2.HasFile
            && m_link_dinh_kem12.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem12.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_12.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }

        if ((m_up_ho_so_loai_1_dk3.HasFile)
            && m_link_dinh_kem13.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem13.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_13.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }

        if ((m_up_ho_so_loai_2_dk1.HasFile)
            && m_link_dinh_kem21.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem21.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_21.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }
        if ((m_up_ho_so_loai_2_dk2.HasFile)
            && m_link_dinh_kem22.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem22.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_22.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }
        if ((m_up_ho_so_loai_2_dk3.HasFile)
            && m_link_dinh_kem23.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem23.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_23.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }

        if ((m_up_ho_so_loai_3_dk1.HasFile)
            && m_link_dinh_kem31.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem31.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_31.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }
        if ((m_up_ho_so_loai_3_dk2.HasFile)
            && m_link_dinh_kem32.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem32.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_32.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }
        if ((m_up_ho_so_loai_3_dk3.HasFile)
            && m_link_dinh_kem33.Visible)
        {
            m_us_gd_ho_so_gv_detail.dcID = CIPConvert.ToDecimal(m_link_dinh_kem33.ToolTip);
            m_us_gd_ho_so_gv_detail.strHO_SO_DINH_KEM = hdf_upload_33.Value;
            m_us_gd_ho_so_gv_detail.update_link_dinh_kem();
        }
        m_us_gd_ho_so_gv_detail.CommitTransaction();

        load_all_hs_dinh_kem();
    }
    #endregion

    #region Events
    protected void m_cmd_thoat_Click(object sender, EventArgs e)
    {
        try
        {
            reset_control();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.Equals(this, v_e);
        }
    }
    protected void m_grv_gd_ho_so_gv_detail_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        try
        {
            m_init_mode = DataEntryFormMode.UpdateDataState;
            m_cmd_cap_nhat_pl.Enabled = true;
            m_lbl_thong_bao.Text = "";
            //m_pnl_table.Visible = true;
            load_data_2_us_by_id_and_show_on_form(e.NewSelectedIndex);
        }
        catch (Exception V_e)
        {
            CSystemLog_301.ExceptionHandle(this, V_e);
        }
    }
    protected void m_grv_gd_ho_so_gv_detail_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        try
        {
            m_lbl_thong_bao.Text = "";
            delete_row_ho_so_gv_detail(e.RowIndex);
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_cap_nhat_pl_Click(object sender, EventArgs e)
    {
        try
        {
            Upload_dinh_kem_ho_so();
            update_file_ho_so_dinh_kem();
            m_lbl_mess.Text = "Cập nhật bản ghi thành công";
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cmd_exit_Click(object sender, EventArgs e)
    {
        try
        {
            Response.Redirect("/TRMProject/DanhMuc/F106_HoSo.aspx", false);
            HttpContext.Current.ApplicationInstance.CompleteRequest();
        }
        catch (Exception v_e)
        {

            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_cbo_loai_ho_so_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {

        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    protected void m_cmd_cap_nhat_trang_thai_Click(object sender, EventArgs e)
    {
        try
        {
            cap_nhat_trang_thai_gv_theo_don_vi_qly();
        }
        catch (Exception v_e)
        {
            CSystemLog_301.ExceptionHandle(this, v_e);
        }
    }
    protected void m_grv_dm_ho_so_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.DataItem != null)
                {
                    Label lblVanBangCongChungYN = ((Label)e.Row.FindControl("VanBangCongChungYN"));
                    Label lblCCSPYN = ((Label)e.Row.FindControl("CCSPYN"));
                    Label lblLyLichKhoaHocYN = ((Label)e.Row.FindControl("LyLichKhoaHocYN"));

                    CheckBox v_chb_VanBangCongChung = (CheckBox)e.Row.FindControl("chk_van_bang_cong_chung");
                    CheckBox v_chb_CCSP = (CheckBox)e.Row.FindControl("chk_ccsp");
                    CheckBox v_chb_LyLichKhoaHoc = (CheckBox)e.Row.FindControl("chk_ly_lich_khoa_hoc");

                    if (lblVanBangCongChungYN.Text.Trim().Equals("Y"))
                        v_chb_VanBangCongChung.Checked = true;
                    if (lblCCSPYN.Text.Trim().Equals("Y"))
                        v_chb_CCSP.Checked = true;
                    if (lblLyLichKhoaHocYN.Text.Trim().Equals("Y"))
                        v_chb_LyLichKhoaHoc.Checked = true;
                }
            }
        }
        catch (Exception v_e)
        {
            throw v_e;
        }
    }
    #endregion
}